package com.sunrise.xdoc.view.system;

import org.apache.commons.lang3.StringUtils;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import java.io.Serializable;

/**
 * The Class LeftMenuBean.
 *
 * @author Nguyen Duc Dung
 * @since 4/22/13 1:44 PM
 */
@ManagedBean
@SessionScoped
public class LeftMenuBean implements Serializable {

    public String getClass(String page) {
        String viewId = getViewId();
        if(viewId != null && viewId.equals(page)) {
            return "ui-state-active";
        }
        return StringUtils.EMPTY;
    }

    private String getViewId() {
        FacesContext fc = FacesContext.getCurrentInstance();
        String viewId = fc.getViewRoot().getViewId();
        String selectedComponent;

        if (viewId != null) {
            selectedComponent = viewId.substring(viewId.lastIndexOf("/") + 1, viewId.lastIndexOf("."));
        } else {
            selectedComponent = null;
        }

        return selectedComponent;
    }

}
